//
//  DataExtension.swift
//  Pintuan
//
//  Created by yiwanjun on 2017/8/29.
//  Copyright © 2017年 jwsszle, Inc. All rights reserved.
//

import Foundation
import UIKit

public extension Double {
    /// SwifterSwift: String.
    var twoPointsString: String {
        return String(format: "%.2f", self)
    }
}

/// 抽象数字类型，使他们能够执行统一方法
public protocol DigtalTarget {}

extension Int: DigtalTarget {
    
}
extension UInt: DigtalTarget {
    
}

extension Float: DigtalTarget {
}

extension Double: DigtalTarget {
}

extension CGFloat: DigtalTarget {
    
}
extension NSNumber: DigtalTarget{
    
}

public extension DigtalTarget {
    //根据当前屏幕宽度和设计标准宽度（375）计算得到适配后的数值，可以是宽度和高度，一般用于特殊布局。
    var adapt: CGFloat {
        return medium * (UIScreen.main.bounds.width / 375.0)
    }
    //保留1位小数
    var onePoints: CGFloat {
        return CGFloat(floorf(Float(medium * 10)) / 10)
    }
    //保留2位小数
    var twoPoints: CGFloat {
        return CGFloat(floorf(Float(medium * 100)) / 100)
    }
    
    var medium:CGFloat {
        let offset: CGFloat
        if let amount = self as? Float {
            offset = CGFloat(amount)
        } else if let amount = self as? Double {
            offset = CGFloat(amount)
        } else if let amount = self as? CGFloat {
            offset = CGFloat(amount)
        } else if let amount = self as? Int {
            offset = CGFloat(amount)
        } else if let amount = self as? UInt {
            offset = CGFloat(amount)
        } else {
            offset = 0.0
        }
        return offset
    }
    
    /// - Returns: 带两位小数的字符串
    var priceString: String{
        let price = self.twoPoints
        return String(format: "%0.2f", Float(price))
    }
    /// - Returns: 带有货币符号的字符串
    var localPrice: String {
        let price = self.twoPoints
        return String(format: "￥%0.2f", Float(price))
    }
    
    /// - Returns: 带有正负号的和货币符号的字符串
    var pmLocalPrice: String {
        let price = self.twoPoints
        var flag = ""
        if price > 0 {
            flag = "+"
        }else if price < 0{
            flag = "-"
        }
        return String(format: "%@￥%0.2f", flag, Float(abs(price)))
    }
}

extension DigtalTarget {
    /// 不做任何处理转化成字符串
    /// - Returns: 字符串
    var string: String {
        return "\(self)"
    }
}


